package com.example.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.pojo.Order;
import com.example.pojo.Product;
import com.example.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

/**
 * @author yinyin
 * @create 2021/09/07 下午 12:16
 */
@Slf4j
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private RestTemplate restTemplate;
    @Override
    @SentinelResource(value = "selectProductById",
            blockHandler = "selectProductByIdBlockHandler",
            fallback = "selectProductByIdFallback"
    )
    public Product selectProductById(Integer id) {
        return restTemplate.getForObject("http://product-service/product/"+id, Product.class);
    }

    //服务流量控制处理,参数最后多个BlockException  其余与原函数一致
    public Product selectProductByIdBlockHandler(Integer id, String orderNo, BlockException ex){
        ex.printStackTrace();
        return new Product(id, "服务流量控制处理-托底数据", 1, 2666D);
    }

    //服务熔断降级处理
    public Product selectProductByIdFallback(Integer id,String orderNo,Throwable throwable){
        log.error("order-service服务的selectOrderById 方法出现异常,异常信息如下:"+throwable);
        return new Product(id, "服务熔断降级处理-托底数据", 1, 2666D);
    }
}
